package com.mobi.Survey.lib.db;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.mobi.Survey.lib.model.Pojo;
import com.mobi.Survey.lib.model.SurveyDetailPojo;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * SurveyResultTable.java This class provide interfaces for operation on Survey
 * Result Table
 * 
 */
public class SurveyResultTable extends AbstractTable {

	public static final String TABLE = "survey_result";

	public static final String COL_SURVEY_ID = "survey_id";
	public static final String COL_CTRL_TYPE = "ctrl_type";
	public static final String COL_CTRL_USER_VALUE = "ctrl_user_value";
	public static final String COL_DEV_NUM = "device_number";
	public static final String COL_SUBS_ID = "subs_id";
	public static final String COL_SEQ_NO = "seq_no";

	public SurveyResultTable() {

	}

	public SurveyResultTable(Context ctx) {
		super(ctx);
	}

	/**
	 * insert data for single control of Survey Controls
	 */
	public long insertRow(Pojo obj, long surveyId) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		SurveyDetailPojo sdp = (SurveyDetailPojo) obj;
		sdp.setSurveyId(surveyId);
		ContentValues cv = new ContentValues();
		cv.put(COL_SURVEY_ID, sdp.getSurveyId());
		cv.put(COL_CTRL_TYPE, sdp.getControlType());
		cv.put(COL_CTRL_USER_VALUE, sdp.getUserValue());
		cv.put(COL_SUBS_ID, sdp.getSubscriptionId());
		cv.put(COL_DEV_NUM, SurveyDetailPojo.getDeviceId());
		cv.put(COL_SEQ_NO, sdp.getSequanceNo());

		return db.insert(TABLE, null, cv);
	}

	/**
	 * Writes user and actual answer on given SurveyDetailPojos
	 * 
	 * @param surveyId
	 * @param sdps
	 */
	public void writeResult(long surveyId, List<SurveyDetailPojo> sdps) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();

		Cursor cursor = db.query(TABLE, null, COL_SURVEY_ID + " =? ",
				new String[] { surveyId + "" }, null, null, null);

		if (cursor != null) {

			if (cursor.moveToFirst() && cursor.getCount() > 0) {
				int index = 0;
				byte ctr_type = 0;

				while (!cursor.isAfterLast()) {
					// Control Type
					ctr_type = (byte) cursor.getInt(1);
					// We dont have answers about ImageView, URL and Video View
					// so we are skipping them
					if (ctr_type == sdps.get(index).getControlType()) {

						String usrVal = cursor.getString(3);
						sdps.get(index).setUsertValue(usrVal);
						index++;

					} else {
						index++;
						continue;
					}

					cursor.moveToNext();
				}

			}
			cursor.close();
			cursor = null;
		}

	}

	@Override
	public long insertRow(Pojo obj) {

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		SurveyDetailPojo sdp = (SurveyDetailPojo) obj;
		ContentValues cv = new ContentValues();
		cv.put(COL_SURVEY_ID, sdp.getSurveyId());
		cv.put(COL_CTRL_TYPE, sdp.getControlType());
		cv.put(COL_CTRL_USER_VALUE, sdp.getUserValue());
		cv.put(COL_DEV_NUM, SurveyDetailPojo.getDeviceId());
		cv.put(COL_SUBS_ID, sdp.getSubscriptionId());
		cv.put(COL_SEQ_NO, sdp.getSequanceNo());
		return db.insert(TABLE, null, cv);
	}

	@Override
	public boolean createTable(SQLiteDatabase sqliteDb) {
		String query = "create table " + TABLE + "( " + COL_SURVEY_ID
				+ " long ,  " + COL_CTRL_TYPE + " text, " + COL_DEV_NUM
				+ " text, " + COL_CTRL_USER_VALUE + " text, " + COL_SUBS_ID
				+ " text," + COL_SEQ_NO + " integer )";
		sqliteDb.execSQL(query);
		printMessage(query);
		return true;
	}

	/**
	 * 
	 * @param surveyId
	 * @return 0 if new
	 */
	public int isNew(long surveyId) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(TABLE, new String[] { COL_SURVEY_ID },
				COL_SURVEY_ID + "= ? ", new String[] { surveyId + "" }, null,
				null, null);
		int ret = 0;
		if (cursor != null) {

			ret = cursor.getCount();

			cursor.close();
			cursor = null;
		}
		return ret;

	}

	@Override
	public boolean deleteRow(long id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int ret = db.delete(TABLE, COL_SURVEY_ID + " = ? ", new String[] { id
				+ "" });
		return ret > 0;
	}

	@Override
	public void deleteAllData() {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.delete(TABLE, null, null);

	}

}
